Sortings Quick Insertion Selection
class Sorting{
public int Partition(int[] arr,int low,int high){
int i, pivotloc,pivotkey;
//swap(arr,low,(low+high)/2);
pivotkey = arr[low];
pivotloc = low;
for(i=low+1;i<=high;i++){
if(arr[i]<pivotkey){
swap(arr,++pivotloc,i);
}
}
swap(arr,low,pivotloc);
return pivotloc;
}
void QuickSort(int [] arr,int low,int high){
int pivotloc;
if(low< high){
pivotloc = Partition(arr,low,high);
QuickSort(arr,low,pivotloc-1);
QuickSort(arr,pivotloc+1,high);
}
}
public void insertionSort(int[] arr1){
int[] arr = arr1;
int i,j,item;
for(i=1;i<arr.length;i++){
item = arr[i];
for(j=i-1;j>=0 && arr[j]>item;j--){
arr[j+1] = arr[j];
}
arr[j+1] = item;
}
//return arr;
}
public void selectionSort(int[] arr1){
int[] arr = arr1;
int i,largest,n;
n = arr.length;
for(i=0;i<n;i++){
largest = find_largest(arr,n-(i));
System.out.print("\nLargest: "+largest);
swap(arr,largest,n-(i+1));
}
}
public int find_largest(int[] arr, int high){
int i,max = arr[0],indx=0;
for(i=1;i<high;i++){
if(arr[i]>max){
max = arr[i];
indx = i;
}
}
return indx;
}
public void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
class sorting_demo{
public static void display_arr(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(" "+arr[i]);
}
}
public static void main(String[] op){
int[] arr = {300,3,4,5,1,-99,19,12,333,22};
Sorting s = new Sorting();
System.out.print("InsertionSort");
s.insertionSort(arr);
display_arr(arr);
int[] arr1 = {3,4,5,1,23,33,19,10};
System.out.print("\nSelectionSort");
display_arr(arr1);
s.selectionSort(arr1);
display_arr(arr1);
System.out.println("\nQuickSort: ");
int[] arr2 = {26,26,33,48,27,38,35,29,19,12,22};
Sorting qs = new Sorting();
qs.QuickSort(arr2,0,arr2.length-1);
display_arr(arr2);
}
}